const resolve = require('path').resolve
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const url = require('url')
const publicPath = ''

//报错解决方法(node:4884) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic, 
//see https://github.com/webpack/loader-utils/issues/56
process.traceDeprecation = true
process.noDeprecation = true

module.exports = (options = {}) => ({	
  entry: {
    vendor: './src/vendor',
    index: './src/main.js',
  },
  output: {
    path: resolve(__dirname, 'dist'),
    filename: options.dev ? '[name].js' : '[name].js?[chunkhash]',
    chunkFilename: '[id].js?[chunkhash]',
    publicPath: options.dev ? '/assets/' : publicPath
  },
  module: {
    rules: [{
        test: /\.vue$/,
        use: ['vue-loader']
      },
      {
	test: /\.js$/,
        loader: 'babel-loader',
        options: {
          presets: ['es2015']
        },
        include: [
			resolve('src'),
			// resolve('test'),
		    resolve('node_modules/element-ui/packages')
		]
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader']
      },
      {
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 10000
          }
        }]
      }
    ]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor', 'manifest']
    }),
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ],
  resolve: {
    alias: {
      '~': resolve(__dirname, 'src')
    },
    extensions: ['.js', '.vue', '.json', '.css']
  },
  devServer: {
    host: '127.0.0.1',
    port: 8010,
    proxy: {
		'/api/': {
			target: 'http://wutong.izhuodata.top/',
			changeOrigin: true,
			pathRewrite: {
				'^/api': '/api'
			}
		},
		// '/json': {
		// 	target: 'http://wutong.izhuodata.top', //你要访问的服务器域名
		// 	changeOrigin: true, //允许跨域
		// 	pathRewrite: {
		// 		'^/json': ''
		// 	}
		// }
    },
    historyApiFallback: {
      index: url.parse(options.dev ? '/assets/' : publicPath).pathname
    }
  },
  devtool: options.dev ? '#eval-source-map' : '#source-map'
})